package logic

import (
	"context"
	"fmt"
	"srv/model/mysql"
	"srv/model/redis"

	"srv/cart"
	"srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddGoodLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodLogic {
	return &AddGoodLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddGoodLogic) AddGood(in *cart.AddGoodRequest) (*cart.AddGoodResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Cart{
		UserId:    int(in.UserId),
		GoodTitle: in.GoodTitle,
		GoodCarve: in.GoodCarve,
		GoodPrice: float64(in.GoodPrice),
		GoodStock: int(in.GoodStock),
		GoodState: int(in.GoodState),
		GoodTypes: int(in.GoodTypes),
	}
	err := g.Create()
	if err != nil {
		logx.Error("添加商品失败")
		return nil, fmt.Errorf("添加商品失败")
	}

	redis.StockGoods(int64(g.ID), in.GoodStock)

	return &cart.AddGoodResponse{GoodsId: int64(g.ID)}, nil
}
